//
//  WaterfallViewController.m
//  CustomDemo
//
//  Created by 余意 on 2016/12/20.
//  Copyright © 2016年 余意. All rights reserved.
//

#import "WaterfallViewController.h"

#import "WaterfallLayout.h"

@interface WaterfallViewController () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) WaterfallLayout * layout;

@property (nonatomic,strong) UICollectionView * WFCollectionView;

@end

@implementation WaterfallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self WFCollectionView];
    
    [self.layout computeIndexCellHeightWithWidthBlock:^CGFloat(NSIndexPath *indexPath, CGFloat width) {
        int randomNum = arc4random_uniform(400);
        return (CGFloat)randomNum;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Lazy Load

- (WaterfallLayout *)layout
{
    if (!_layout)
    {
        /*
         lineNum      列数
         rowSpace     垂直间距
         lineSpace    水平间距
         sectionInset collectionView边距
         */
        _layout = [[WaterfallLayout alloc] init];
        _layout.lineNum = 5;
        _layout.rowSpace = 10;
        _layout.lineSpace = 10;
        _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    }
    return _layout;
}

- (UICollectionView *)WFCollectionView
{
    if (!_WFCollectionView)
    {
        _WFCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:self.layout];
        [_WFCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
        _WFCollectionView.delegate = self;
        _WFCollectionView.dataSource = self;
        _WFCollectionView.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:_WFCollectionView];
    }
    return _WFCollectionView;
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 100;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [self.WFCollectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = RANDOMCOLOR;
    return cell;
}



@end
